classdef ziDaqData < EquipmentControl.ZI.ziData.ziData
    %ZIDAQDATA Summary of this class goes here
    %   Detailed explanation goes here
    
    properties
        default_channelName
        channel_Name
    end
    
    methods
        function obj = ziDaqData(data, varargin)
            %ZIDAQDATA Construct an instance of this class
            %   Detailed explanation goes here
            obj@EquipmentControl.ZI.ziData.ziData(data);
            obj.default_channelName = 'demods';
            obj.channel_Name = obj.default_channelName;
            
%             p=inputParser;
%             p.addParameter('channel_name', 'demods');
%             p.addParameter('sample_name', 'sample_r_avg');
%             p.parse(varargin{:});
%             
%             obj.channel_Name = p.Results.channel_name;
%             obj.sampleName = p.Results.sample_name;
%             obj.default_component = 'value';
%             
%             obj.nNode = length(data.(obj.device).(obj.channel_Name));
%             obj.nSample = zeros(1, obj.nNode);
% 
%             for k=1:obj.nNode
%                 obj.nSample(k) = length(data.(obj.device)...
%                                             .(obj.channel_Name)(k)...
%                                             .(obj.sampleName));
%             end
        end
        
        function data = getNodeData(obj, channelIndex, sampleName, sampleIndex)
            if nargin == 3
                sampleIndex = 1;
            end
            
            if ischar(channelIndex)
                chIdx = str2double(channelIndex) + 1;
            else
                chIdx = channelIndex;
            end
            
            sa_name = lower(replace(sampleName, '.', '_'));
            
            if contains(sa_name, 'fft')
                data.frequency = obj.getFrequencyArray(obj.channel_Name, chIdx, sa_name, sampleIndex);
                data.value = obj.getData(obj.channel_Name, chIdx, sa_name, sampleIndex, 'value');
                data.label = ['demods/' channelIndex '/' sampleName];
            else
                data.time = obj.getTimeArray(obj.channel_Name, chIdx, sa_name, sampleIndex);
                data.value = obj.getData(obj.channel_Name, chIdx, sa_name, sampleIndex, 'value');
                data.label = ['demods/' channelIndex '/' sampleName];
            end

        end      
        
        function time = getTimeArray(obj, chName, chIdx, sa_name, sa_index)
            time1 = double(obj.getData(chName, chIdx, sa_name, sa_index, 'timestamp'))./obj.clockbase;
            time = time1 - time1(1);
        end
        
        function freq = getFrequencyArray(obj, chName, chIdx, sa_name, sa_index)
            header = obj.data.(obj.device).(chName)(chIdx).(sa_name){sa_index}.header;
            df = header.gridcoldelta;
            freq = df*double(0:(header.gridcols-1)).';
        end

        %% deprecated
        function res = getArray(obj, varargin)
            [content, label] = obj.get_content(varargin{:});
            res.label = label;
            res.x = obj.get_grid_col_array(content);
            res.y = [];
            for k = 1:length(label)
                res.y = [res.y; content.(label{k})];
            end
            
        end
        
        function showData(obj, varargin)
            res = obj.getArray(varargin{:});
            figure;plot(res.x, res.y);
            legend(res.label);
        end
    end
end

